<{include file="public/header" /}>
<style>
  .layui-table tbody tr:hover {
      background-color: transparent;
  }
  .layui-table-cell{
    height:auto;
    /*line-height:50;*/
    text-overflow: inherit;
    white-space: normal;
}
.layui-tab{margin-top: 2px;}
</style>
<body>
<div class="layui-tab layui-tab-brief">
  <ul class="layui-tab-title">
    <li>已考题库</li>
    <li class="layui-this">添加新题</li>
  </ul>
  <div class="layui-tab-content">
    <div class="layui-tab-item">
      <!-- 顶部搜索 -->
      <div class="layui-form">
        <div class="layui-form-item" style="margin-top: 10px;">
          <label class="layui-form-label" style="width: 60px;">考试历史</label>
          <div class="layui-input-inline">
            <select name="examhistroy" lay-filter="examhistroy" id="examhistroy">
              <option value="" qid=""></option>
              <{volist name="exams" id="exam"}>
              <option value="<{$exam.eid}>" qid="<{$exam.qids}>"><{$exam.title}></option>
              <{/volist}>
            </select>
          </div>
          <label class="layui-form-label"style="width: 30px;">题型</label>
          <div class="layui-input-inline" >
            <select name="qtypetiku" lay-filter="qtypetiku" id="qtypetiku">
              <option value=""></option>
              <option value="5">标题</option>
              <option value="1">单选</option>
              <option value="2">多选</option>
              <option value="3">填空</option>
              <option value="4">计算</option>
            </select>
          </div>
         <!--  <label class="layui-form-label" style="width: 30px;">科目</label>
          <div class="layui-input-inline">
            <select name="subjecttiku" lay-filter="subjecttiku" id="subjecttiku">
              <option value=""></option>
              <option value="1">语文</option>
              <option value="2">数学</option>
              <option value="3">英语</option>
              <option value="4">物理</option>
              <option value="5">化学</option>
              <option value="6">生物</option>
              <option value="7">政治</option>
              <option value="8">历史</option>
              <option value="9">地理</option>
            </select>
          </div> -->
          <button class="layui-btn" data-type="reload" id="searchku"><i class="layui-icon layui-icon-search"></i>搜索</button>
          
        </div>
      </div>
      <!-- 顶部搜索end -->
      <table class="layui-hide" id="tiku" lay-filter="tiku" ></table>
    </div>
    <div class="layui-tab-item layui-show">
      <!-- 顶部搜索 -->
      <div class="layui-form">
        <div class="layui-form-item" style="margin-top: 10px;">
          <label class="layui-form-label">题型</label>
          <div class="layui-input-inline" >
            <select name="qtype" lay-filter="qtype" id="qtype">
              <option value=""></option>
              <option value="5">标题</option>
              <option value="1">单选</option>
              <option value="2">多选</option>
              <option value="3">填空</option>
              <option value="4">计算</option>
            </select>
          </div>
          <!-- <label class="layui-form-label" style="width: 30px;">科目</label>
          <div class="layui-input-inline">
            <select name="subject" lay-filter="subject" id="subject">
              <option value=""></option>
              <option value="1">语文</option>
              <option value="2">数学</option>
              <option value="3">英语</option>
              <option value="4">物理</option>
              <option value="5">化学</option>
              <option value="6">生物</option>
              <option value="7">政治</option>
              <option value="8">历史</option>
              <option value="9">地理</option>
            </select>
          </div> -->
          <button class="layui-btn" data-type="reload" id="search"><i class="layui-icon layui-icon-search"></i>搜索</button>
          <button class="layui-btn" data-type="reload" id="refresh"><i class="layui-icon layui-icon-refresh"></i>刷新</button>
        </div>
      </div>
      <!-- 顶部搜索end -->
      <table class="layui-hide" id="shiti" lay-filter="shiti" ></table>

    </div>
  </div>
</div>


<!-- 题库表头工具 -->
<script type="text/html" id="headertoolbarku">
  <div class="layui-btn-container">
    <!-- <button class="layui-btn layui-btn-sm" lay-event="addData"><i class="layui-icon layui-icon-add-circle"></i>添加试题</button> -->
    <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="getCheckData"><i class="layui-icon layui-icon-add-circle
"></i>添加到试卷</button>
  </div>
</script>
<script type="text/html" id="linetoolbarku">
  <!-- <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="banli">办理</a> -->
  <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>   
<!-- 题库表头工具end -->
<!-- 添加新题头工具 -->
<script type="text/html" id="headertoolbar">
  <div class="layui-btn-container">
    <button class="layui-btn layui-btn-sm" lay-event="addData"><i class="layui-icon layui-icon-add-circle"></i>添加试题</button>
    <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="getCheckData"><i class="layui-icon layui-icon-add-circle
"></i>添加到试卷</button>
  </div>
</script>
<script type="text/html" id="linetoolbar">
  <!-- <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="banli">办理</a> -->
  <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script> 
<!-- 添加新题头工具end -->  
<!-- 公用题目模板 -->
<script type="text/html" id="questionTpl">
  <b>{{ d.qtitle }}</b> &nbsp;&nbsp;<!-- {{ get_question_subject(d.qsubject) }} -->&nbsp;&nbsp;{{ get_question_type(d.qtype) }}&nbsp;&nbsp;<i class="layui-icon layui-icon-username"></i>&nbsp;{{ d.getteacher.tname }}
  <div class="layui-row">
   {{ get_answers(d) }} 
  </div>
  <div class="layui-row">
    {{ get_right_answer(d) }}
  </div>
</script> 
<!-- 公用题目模板end -->  
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 --> 
<script>
layui.use(['form','laypage','table','element','jquery'], function(){
  var table = layui.table,laypage=layui.laypage,element=layui.element,$ = layui.$,form=layui.form;
form.render(); 
  $('#refresh').on('click',function(){
      window.location.reload()
    });
  //题库数据表格配置
  table.render({
    elem: '#tiku'
    ,url:'<{:url('qdata_tiku')}>'
    ,toolbar: '#headertoolbarku'
    ,defaultToolbar: ['print', 'exports']
    ,title: '试题题库_siku在线考试系统'
    ,text:{none:'暂无您搜索题型'}
    ,cols: [[
      {type: 'checkbox'}
      ,{field:'qid', title:'ID',width:'6%', sort: true}
      ,{field:'qdata', title:'试题',width:'74%', sort: true,templet:'#questionTpl'}
      // ,{field:'uname', title:'用户名'}
      // ,{field:'truename', title:'姓名'}
      // ,{field:'admintype', title:'权限',templet:function(d){return check_admin(d.admintype)}}
      // ,{field:'class', title:'班级', sort: true}
      // ,{field:'grade', title:'年级',sort: true,templet:function(d){return check_grade(d.grade)}}
      // ,{field:'create_time', title:'时间',width:160,sort: true}
      ,{title:'操作', toolbar: '#linetoolbarku', width:'20%'}
    ]]
    ,page: true
    ,id: 'hereReloadku'
  });
   //新加题目数据表格配置
  table.render({
    elem: '#shiti'
    ,url:'<{:url('qdata')}>'
    ,toolbar: '#headertoolbar'
    ,defaultToolbar: ['print', 'exports']
    ,title: '试题表_siku在线考试系统'
    ,text:{none:'尚未录入新题'}
    ,cols: [[
      {type: 'checkbox'}
      ,{field:'qid', title:'ID',width:'6%', sort: true}
      ,{field:'qdata', title:'试题',width:'74%', sort: true,templet:'#questionTpl'}
      // ,{field:'uname', title:'用户名'}
      // ,{field:'truename', title:'姓名'}
      // ,{field:'admintype', title:'权限',templet:function(d){return check_admin(d.admintype)}}
      // ,{field:'class', title:'班级', sort: true}
      // ,{field:'grade', title:'年级',sort: true,templet:function(d){return check_grade(d.grade)}}
      // ,{field:'create_time', title:'时间',width:160,sort: true}
      ,{title:'操作', toolbar: '#linetoolbar', width:'16%'}
    ]]
    ,page: true
    ,id: 'hereReload'
  });


  //tiku头工具栏事件
  table.on('toolbar(tiku)', function(obj){
    //console.log(obj);
    var checkStatus = table.checkStatus(obj.config.id);
    switch(obj.event){
      case 'getCheckData':
        var data = checkStatus.data;
        //layer.alert(JSON.stringify(data));
        //console(data.qid);
        var id_str='';
      if (data.length > 0) {
        for (var i = 0; i < data.length; i++) {
        id_str += data[i].qid + ',';
        }
        id_str = id_str.substr(0, id_str.length-1);//获取选中id
        
        //alert(id_str);
        //异步批量添加试题到试卷
            var str='<{:url("exam/add_q_to_exam",["id"=>"tid"])}>';
            var url=str.replace("tid",id_str);
           layer.open({
            title:'添加试题到试卷/考试'
            ,type: 2
            //offset: '10px'
            ,area: ['500px', '450px']
            ,content: url//这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
          }); 
      }else{
        layer.msg('没有选中试题！')
      }
       //  ,function(){
       //    //点击取消的返回值
       //    //layer.msg('quxiao')
       //    window.location.reload()
       // });
    //异步批量删除选中的数据end
      break;
      case 'addData':
        layer.open({
          title:'添加试题'
      ,type: 2, 
      offset: '10px'
      ,area: ['900px', '450px']
      ,content: '<{:url('add')}> '//这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
    }); 
      break;
    };
  });
  
  //tiku监听行工具事件
  table.on('tool(tiku)', function(obj){
    var data = obj.data;
    //console.log(obj);
    //alert(data.tid);
    if(obj.event === 'del'){
      //alert(data.qid);
      layer.confirm('真的删除该题吗？', function(index){
         $.post("<{:url('del')}>",{id:data.qid},function(data,status){
            //alert(data);
            //alert(JSON.stringify(data));
            //console(data);
            if(data===1){
              layer.close(index);
              obj.del();
              layer.msg('删除成功！',{time:1000})
            }else{
              layer.close(index);
              layer.alert('删除数据错误，请刷新重试！',{icon:5});
            }
       })
      });
    } else if(obj.event === 'edit'){
      var data=obj.data;
      //alert(data.qtype);
      var edit = check_edittype(data.qtype);
      var str='<{:url("edit",["id"=>"tid"])}>';
      var url1=str.replace("tid",data.qid);
      var url=url1.replace("edit",edit);
      //alert(url);
      layer.open({
          title:'修改试题'
      ,type: 2 
      ,offset: '10px'
      ,area: ['900px', '450px']
      ,content: url
      })
    }else if (obj.event ==='banli') {
          var data=obj.data;
          if (data.status==1) {
            layer.confirm('确定标记为&nbsp;<b style="color:#FF5722;">未办理</b>&nbsp;吗？', {btn: ['确定','取消'],icon: 3, title:'提示'}
              ,function(index){
                //do something
                  $.post("<{:url('change_status')}>",{id:data.id,status:2},function(data,status){
                    //alert(data);
                    //alert(JSON.stringify(data));
                    //console(data);
                    if(data===1){
                      layer.close(index);
                      layer.msg('已标记为未办理！',{time:1000},function(){
                        obj.update({
                          status: 2
                        });
                      }); 
                    }else{
                      layer.close(index);
                      layer.alert('标记失败，请刷新重试！',{icon:5});
                    }
                 })
                
              });
          } else {
              layer.confirm('确定标记为&nbsp;<b style="color: #009688;">已办理</b>&nbsp;吗？', {btn: ['确定','取消'],icon: 3, title:'提示'}
              ,function(index){
                //do something
                  $.post("<{:url('change_status')}>",{id:data.id,status:1},function(data,status){
                    //alert(data);
                    //alert(JSON.stringify(data));
                    //console(data);
                    if(data===1){
                      layer.close(index);
                      layer.msg('已标记为已办理！',{time:1000},function(){
                        obj.update({
                          status: 1
                        });
                      }); 
                    }else{
                      layer.close(index);
                      layer.alert('标记失败，请刷新重试！',{icon:5});
                    }
                 })
                
              });
          }
          //console.log(obj.tr);
        };
  });
  //添加新试题头工具栏事件
  table.on('toolbar(shiti)', function(obj){
    //console.log(obj);
    var checkStatus = table.checkStatus(obj.config.id);
    switch(obj.event){
      case 'getCheckData':
        var data = checkStatus.data;
        //layer.alert(JSON.stringify(data));
        //console(data.qid);
        var id_str='';
      if (data.length > 0) {
        for (var i = 0; i < data.length; i++) {
        id_str += data[i].qid + ',';
        }
        id_str = id_str.substr(0, id_str.length-1);//获取选中id
        
        //alert(id_str);
        //异步批量添加试题到试卷
            var str='<{:url("exam/add_q_to_exam",["id"=>"tid"])}>';
            var url=str.replace("tid",id_str);
           layer.open({
            title:'添加试题到试卷/考试'
            ,type: 2
            //offset: '10px'
            ,area: ['500px', '450px']
            ,content: url//这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
          }); 
      }else{
        layer.msg('没有选中试题！')
      }
       //  ,function(){
       //    //点击取消的返回值
       //    //layer.msg('quxiao')
       //    window.location.reload()
       // });
    //异步批量删除选中的数据end
      break;
      case 'addData':
        layer.open({
          title:'添加试题'
      ,type: 2, 
      offset: '10px'
      ,area: ['900px', '450px']
      ,content: '<{:url('add')}> '//这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
    }); 
      break;
    };
  });
  
  //shiti监听行工具事件
  table.on('tool(shiti)', function(obj){
    var data = obj.data;
    //console.log(obj);
    //alert(data.tid);
    if(obj.event === 'del'){
      //alert(data.qid);
      layer.confirm('真的删除该题吗？', function(index){
         $.post("<{:url('del')}>",{id:data.qid},function(data,status){
            //alert(data);
            //alert(JSON.stringify(data));
            //console(data);
            if(data===1){
              layer.close(index);
              obj.del();
              layer.msg('删除成功！',{time:1000})
            }else{
              layer.close(index);
              layer.alert('删除数据错误，请刷新重试！',{icon:5});
            }
       })
      });
    } else if(obj.event === 'edit'){
      var data=obj.data;
      //alert(data.qtype);
      var edit = check_edittype(data.qtype);
      var str='<{:url("edit",["id"=>"tid"])}>';
      var url1=str.replace("tid",data.qid);
      var url=url1.replace("edit",edit);
      //alert(url);
      layer.open({
          title:'修改试题'
      ,type: 2 
      ,offset: '10px'
      ,area: ['900px', '450px']
      ,content: url
      })
    }else if (obj.event ==='banli') {
          var data=obj.data;
          if (data.status==1) {
            layer.confirm('确定标记为&nbsp;<b style="color:#FF5722;">未办理</b>&nbsp;吗？', {btn: ['确定','取消'],icon: 3, title:'提示'}
              ,function(index){
                //do something
                  $.post("<{:url('change_status')}>",{id:data.id,status:2},function(data,status){
                    //alert(data);
                    //alert(JSON.stringify(data));
                    //console(data);
                    if(data===1){
                      layer.close(index);
                      layer.msg('已标记为未办理！',{time:1000},function(){
                        obj.update({
                          status: 2
                        });
                      }); 
                    }else{
                      layer.close(index);
                      layer.alert('标记失败，请刷新重试！',{icon:5});
                    }
                 })
                
              });
          } else {
              layer.confirm('确定标记为&nbsp;<b style="color: #009688;">已办理</b>&nbsp;吗？', {btn: ['确定','取消'],icon: 3, title:'提示'}
              ,function(index){
                //do something
                  $.post("<{:url('change_status')}>",{id:data.id,status:1},function(data,status){
                    //alert(data);
                    //alert(JSON.stringify(data));
                    //console(data);
                    if(data===1){
                      layer.close(index);
                      layer.msg('已标记为已办理！',{time:1000},function(){
                        obj.update({
                          status: 1
                        });
                      }); 
                    }else{
                      layer.close(index);
                      layer.alert('标记失败，请刷新重试！',{icon:5});
                    }
                 })
                
              });
          }
          //console.log(obj.tr);
        };
  });
  //题库表格重载
    var activetiku = {
      reload: function(){
        var qtypetiku = $('#qtypetiku'),examhistroy = $('#examhistroy');
        
        //执行重载
        table.reload('hereReloadku', {
          page: {
            curr: 1 //重新从第 1 页开始
          }
          ,where: {
            //key: {
              qtypetiku: qtypetiku.val(),examhistroy:examhistroy.find("option:selected").attr('qid')
            //}
          }
        });
      }
    };
    
    $('#searchku').on('click', function(){
      var typetiku = $(this).data('type');
      activetiku[typetiku] ? activetiku[typetiku].call(this) : '';
    });
    //添加新题表格重载
    var active = {
      reload: function(){
        var qtype = $('#qtype'),subject = $('#subject');
        
        //执行重载
        table.reload('hereReload', {
          page: {
            curr: 1 //重新从第 1 页开始
          }
          ,where: {
            //key: {
              qtype: qtype.val()//,subject:subject.val()
            //}
          }
        });
      }
    };
    
    $('#search').on('click', function(){
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });
});
</script>
<div style="height: 150px;background-color: white;"></div>
</body>
</html>
